package repository

import (
	"github.com/gin-gonic/gin"
	"yak.admin/src/entity"
	"yak.admin/src/repository"
	"yak.admin/src/service"
	"yak.admin/src/util"
)

type UserRepository struct {
	Page      int `form:"page"`
	Pagesize  int `form:"per-page"`
	Province  string
	City      string
	County    string
	IsSuper   bool
	YakLst    string `form:"Condition[yak_lst]"`
	YakLstTxt string `form:"Condition[yak_lst_txt]"`
	YakLstExt string `form:"Condition[yak_lst_ext]"`
	Status    int    `form:"Condition[status]"`
	repository.UserRepository
}

func (repository *UserRepository) FindAll(c *gin.Context) ([]map[string]interface{}, int64) {
	if err := util.DefaultGetValidParams(c, repository); err != nil {
		return []map[string]interface{}{}, 0
	}

	reader := new(service.ReaderQuery).CreateModelQuery(&entity.User{}).
		WithEQ("status", repository.Status).
		WithYakLst(repository.YakLst, repository.YakLstTxt, repository.YakLstExt).
		WithPCC(repository.Province, repository.City, repository.County, repository.IsSuper).
		WithPage(repository.Page).WithPageSize(repository.Pagesize)

	var users []*entity.User
	reader.Reader(&users)
	statusLabels := repository.StatusLabels()

	res := reader.Datalist(users, len(users), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.User)
		return map[string]interface{}{
			"_serial":     reader.AsSerial(index),
			"id":          m.Id,
			"phone":       m.Phone,
			"username":    m.Username,
			"nickname":    m.Nickname,
			"address":     m.Address,
			"company":     m.Company,
			"status":      m.Status,
			"statusLabel": statusLabels[m.Status],
		}
	})

	return res, reader.Count()
}

func (repository *UserRepository) FindAllExcel(c *gin.Context) (map[string]interface{}, []map[string]interface{}) {
	if err := util.DefaultGetValidParams(c, repository); err != nil {
		return map[string]interface{}{}, []map[string]interface{}{}
	}

	reader := new(service.ReaderQuery).CreateModelQuery(&entity.User{}).
		WithEQ("status", repository.Status).
		WithYakLst(repository.YakLst, repository.YakLstTxt, repository.YakLstExt).
		WithPCC(repository.Province, repository.City, repository.County, repository.IsSuper).
		WithPage(1).WithPageSize(50000)

	var users []*entity.User
	reader.Reader(&users)
	statusLabels := repository.StatusLabels()

	header := map[string]interface{}{
		"_serial":     "序号",
		//"id":          "用户ID",
		"phone":       "手机号",
		"username":    "用户名",
		"nickname":    "用户昵称",
		"address":     "区域地址",
		"company":     "所属公司",
		"statusLabel": "状态",
	}

	res := reader.Datalist(users, len(users), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.User)
		return map[string]interface{}{
			"_serial":     reader.AsSerial(index),
			// "id":          m.Id,
			"phone":       m.Phone,
			"username":    m.Username,
			"nickname":    m.Nickname,
			"address":     m.Address,
			"company":     m.Company,
			// "status":      m.Status,
			"statusLabel": statusLabels[m.Status],
		}
	})

	return header, res
}
